<?php

class CmsController extends AdminController
{
    public $parentUrl;
    public function init() {
        parent::init();
        $this->parentUrl = $this->module->id . '/' . Yii::app()->getController()->getId();
    }

	public function actionView($id)
	{
             try{
 		$this->render('view',array(
			'model'=>$this->loadModel($id),'parentUrl'=>$this->parentUrl
		));           
            }catch (Exception $e){
                     Yii::log("Invalid request. Please do not repeat this request again.");
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');              
            }       
	}

	public function actionCreate()
	{
            try{
                    $model=new Cms;
                    if(isset($_POST['Cms'])){
                            $model->attributes=$_POST['Cms'];
                            $model->validate();
                            if(!$model->hasErrors()){
                                
                                $this->saveCms($model);
                                $this->redirect(Yii::app()->createAbsoluteUrl('bookingsystem/cms/index'));                           

                            }      
                    }

                    $this->render('create',array(
                            'model'=>$model,
                    ));            
            }catch (Exception $e){
                    Yii::log("Invalid request. Please do not repeat this request again.");
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');             
            }
	}

	public function actionUpdate($id)
	{
             try{
                    $model=$this->loadModel($id);
                    if(isset($_POST['Cms'])){
                            $model->attributes=$_POST['Cms'];
                            $model->validate();
                            if(!$model->hasErrors()){
                                if($model->save())
                                      $this->redirect(array('view','id'=>$model->id));                           
                            }
                    }

                    $this->render('update',array(
                            'model'=>$model,'parentUrl'=>$this->parentUrl
                    ));           
            }catch (Exception $e){
                     Yii::log("Invalid request. Please do not repeat this request again.");
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');               
            }       
	}
        
	public function actionUpdatelang($page_type,$language_id)
	{
//             try{
                    $model = Cms::model()->findByAttributes(array('page_type'=>$page_type,'language_id'=>$language_id));
//                    $model=$this->loadModel($id);
                    if(isset($_POST['Cms'])){
                            $model->attributes=$_POST['Cms'];
                            $model->validate();
                            if(!$model->hasErrors()){
                                if($model->save())
                                      $this->redirect(array('view','id'=>$model->id));                           
                            }
                    }

                    $this->render('update',array(
                            'model'=>$model,'parentUrl'=>$this->parentUrl
                    ));           
//            }catch (Exception $e){
//                     Yii::log("Invalid request. Please do not repeat this request again.");
//                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');               
//            }       
	}

	public function actionDelete($id)
	{
             try{
                    $model = $this->loadModel($id);
                    if($model){
                        if($model->delete()){
                             $this->redirect(Yii::app()->createAbsoluteUrl($this->parentUrl .'/index'));   
                        }
                    } else throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');            
            }catch (Exception $e){
                    Yii::log("Invalid request. Please do not repeat this request again.");
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');               
            }        
	}

	public function actionIndex()
	{
            try{
                    $model =new Cms();
                    $model->language_id = (isset($_GET['language_id'])) ? $_GET['language_id'] : 1;
                    $data=$model->getAll();
                    $this->render('index',array(
                            'data'=>$data,'parentUrl'=>$this->parentUrl
                    ));           
            }catch (Exception $e){
                     Yii::log("Invalid request. Please do not repeat this request again.");
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');              
            }       

	}

	public function loadModel($id)
	{
             try{
 		$model=Cms::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;           
            }catch (Exception $e){
                    Yii::log("Invalid request. Please do not repeat this request again.");
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');             
            }       
	}

   function saveCms($model){
        $Alllanguage = MyFunctionCustom::getLanguage();   if(count($Alllanguage)>0): 
            foreach ($Alllanguage as $k=>$lang):
                     $cms=new Cms;
                     $cms->name = (isset($_POST['Cms']['name'][$k])) ? $_POST['Cms']['name'][$k] : '';
                     $cms->description = (isset($_POST['Cms']['description'][$k])) ? $_POST['Cms']['description'][$k] : '';
                     $cms->language_id = $k;
                     $cms->page_type   = $_POST['Cms']['page_type'];
                     $cms->save();
            endforeach;
       endif;
   }     
  
        
        
        
        
}
